<?php
/**
 * Created by PhpStorm.
 * User: wjj
 * Date: 2016/11/1
 */

namespace app\logic;


use app\models\Order;
use app\models\Cancel;
use yii;


class OrderLogic
{
    public static $input_type_list = ['下拉框','输入框','单选项','多选项'];

    public function getOrder($query = [], $page = 1, $limit = 10)
    {
        $adv = Order::find()
            ->select('o.*,p.*,g.*,u.*,o.type as type,o.expire_time as expire_time,o.price as price,p.type as play_type')
            ->from('{{%order}} o')
            ->leftJoin('{{%play}} p', 'p.play_id=o.gp_id AND o.type=0')
            ->leftJoin('{{%goods}} g', 'g.goods_id=o.gp_id  AND o.type=1')
            ->leftJoin('{{%user}} u', 'u.user_id=o.user_id')
            ->filterWhere($query)
            ->andWhere("o.status!=0")->orderBy('order_id desc')
            ->offset(($page - 1) * $limit)->limit($limit);
        $lists = $adv->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $adv->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => 'order/index',
                'query' => $query
            ]
        ];
        return $return;
    }

    public function getCancel($query = [], $page = 1, $limit = 10)
    {
        /*$cancel = Cancel::find()
            ->select('c.*,c.type AS type,o.*,s.*,m.manager_name')
            ->from('{{%cancel}} c')
            ->leftJoin('{{%order}} o','o.order_id=c.order_id')
            ->leftJoin('{{%shop}} s','s.shop_id=c.shop_id')
            ->leftJoin('{{%manager}} m','m.manager_id=c.manager_id')
            ->filterWhere($query)->orderBy('cancel_id desc')
            ->offset(($page - 1) * $limit)->limit($limit);*/
        $cancel = Cancel::find()
            ->select('c.cancel_id,c.cancel_time,c.type AS type,o.order_no,s.*,m.manager_name')
            ->from('{{%cancel}} c')
            ->leftJoin('{{%order_cancel}} oc', 'oc.cancel_id=c.cancel_id')
            ->leftJoin('{{%order}} o','o.order_id=oc.order_id')
            ->leftJoin('{{%shop}} s','s.shop_id=c.shop_id')
            ->leftJoin('{{%manager}} m','m.manager_id=c.manager_id')
            ->filterWhere($query)
            /*->andWhere('o.cancel_id is not null')
            ->groupBy('o.cancel_id')*/
            ->orderBy('cancel_id desc')
            ->offset(($page - 1) * $limit)->limit($limit);
        $lists = $cancel->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $cancel->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => 'order/cancel',
                'query' => $query
            ]
        ];
        return $return;
    }

    public function getMoney($type)
    {
        if($type==0){
            $quan = Order::find()
                ->from('{{%order}} o')
                ->leftJoin('{{%play}} p', 'p.play_id=o.gp_id')
                ->where('o.status!=0 and o.type=0 and p.type=0')
                ->sum('o.price');
            return ['quan'=>$quan];
        }

    }




}